﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FB.Bussiness;
using FB.DataAccess;
using FB.Utilities;

namespace FB.admin
{
    public partial class ChanelModify : System.Web.UI.Page
    {
        /// <summary>
        ///  View State EditItemID
        /// </summary>
        private Guid? EditItemID
        {
            get
            {
                object obj = this.ViewState["_EditItemID"];
                if (obj == null)
                    return null; //default to show ID : 0 
                else
                    return (Guid)obj;
            }
            set
            {
                this.ViewState["_EditItemID"] = value;
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPermission();
                LoadForm();
            }
        }

        private void CheckPermission()
        {
            if (Page.User.IsInRole(Constants.ROLE_ADMINISTRATOR)
                || Page.User.IsInRole(Constants.ROLE_AUTHOR)
                || Page.User.IsInRole(Constants.ROLE_CONTRIBUTER)
                || Page.User.IsInRole(Constants.ROLE_EDITOR))
            {
                //
            }
            else
            {
                Response.Redirect("/admin/AccessDenied.aspx");
            }
        }

        private void LoadForm()
        {
            Guid TempID;
            if (Guid.TryParse(Request.QueryString["ID"], out TempID))
            {
                EditItemID = TempID;
            }
            if (EditItemID != null)
            {
                LoadFormEdit();
            }
            else
            {
                LoadFormAdd();
            }
        }

        private void LoadChanelCategory(Guid? SelecteItemID)
        {
            ChanelCategoryBO BO = new ChanelCategoryBO();
            ddlChanelCat.DataSource = BO.GetAllChanelCategory();
            ddlChanelCat.DataTextField = "Name";
            ddlChanelCat.DataValueField = "ID";
            ddlChanelCat.DataBind();
            if (SelecteItemID != null)
            {
                ddlChanelCat.SelectedValue = SelecteItemID.ToString();
            }
        }

        private void LoadFormAdd()
        {
            lblTitle.Text = "Chanel: Add New";
            // Load Chanel category
            LoadChanelCategory(null);
        }

        private void LoadFormEdit()
        {
            lblTitle.Text = "Chanel: Edit";
            ChanelBO BO = new ChanelBO();
            Chanel CurrentChanel = BO.GetChanelById((Guid)EditItemID);
            if (CurrentChanel != null)
            {
                txtName.Text = CurrentChanel.Name;
                txtDisplayName.Text = CurrentChanel.DisplayName;
                LoadChanelCategory(CurrentChanel.CategoryID);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveForm();
            //TODO: Show msg
        }

        
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveForm();
            Response.Redirect("ChanelList.aspx");
        }
        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            SaveForm();
            EditItemID = null;
            Response.Redirect("ChanelModify.aspx?ID=0");
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChanelList.aspx");
        }
        private void SaveForm()
        {
            ChanelBO BO = new ChanelBO();
            //Edit

            if (EditItemID != null)
            {
                Chanel EditChanel = BO.GetChanelById((Guid)EditItemID);
                EditChanel.Name = txtName.Text;
                EditChanel.DisplayName = txtDisplayName.Text;
                EditChanel.CategoryID = Guid.Parse(ddlChanelCat.SelectedValue);
                BO.UpdateChanel(EditChanel);
            }
            else
            {
                Chanel NewChanel = new Chanel();
                NewChanel.ID = Guid.NewGuid();
                NewChanel.Name = txtName.Text;
                NewChanel.DisplayName = txtDisplayName.Text;
                NewChanel.CategoryID = Guid.Parse(ddlChanelCat.SelectedValue);
                NewChanel.CreatedDate = DateTime.Now;
                NewChanel.LastFeedDate = DateTime.Now;
                NewChanel.Enable = true;
                NewChanel.FeedIndex = 1;
                BO.InsertChanel(NewChanel);
            }
        }
    }
}